package com.aliyun.messagemgt.web.aop;

import com.alibaba.cola.dto.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName: GlobalExceptionHandler
 * @Description:
 * @Author: wugongzi
 * @Date: 2021/9/16 14:43
 * @Version: 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Response MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        BindingResult br = e.getBindingResult();
        StringBuilder sb = new StringBuilder();
        br.getAllErrors().forEach(error -> {
            sb.append(error.getDefaultMessage()).append(";");
        });
        return Response.buildFailure("10001", sb.deleteCharAt(sb.length() - 1).toString());
    }
}
